/*
 * DoInscricaoAction.java
 *
 * Created on July 31, 2007, 9:51 AM
 */

package br.usp.poli.mfc.struts.action;

import br.usp.poli.mfc.struts.DAO.DAOFactory;
import br.usp.poli.mfc.struts.DAO.ListaDAO;
import br.usp.poli.mfc.struts.actionform.DoInscricaoActionForm;
import br.usp.poli.mfc.struts.bean.Lista;
import br.usp.poli.mfc.struts.bean.Usuario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
 *
 * @author William T.
 * @version
 */

public class DoInscricaoAction extends Action {
    
    private final static String SUCCESS = "success";
    private static final String ERROR = "error";
    
    private int[] defaultPriority = {100,90,80,70,60,50};
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        ActionMessages messages = new ActionMessages();
        Usuario loggedUser = (Usuario)session.getAttribute("usuario");
        DoInscricaoActionForm frm = (DoInscricaoActionForm) form;
        if(frm!= null){
            Lista addLista = new Lista();
            addLista.setIDRevista(frm.getIdRevista());
            addLista.setIDUsuario(loggedUser.getId_usuario());
            addLista.setPrioridade(defaultPriority[loggedUser.getId_cargo()-1]);
            ListaDAO listaDAO = DAOFactory.getListaDAO();
            //TODO:arrumar essas merdassasasasas
            if (listaDAO.setLista(addLista)) {
                System.out.println("Sucesso ao inserir na lista");
                messages.add("log",new ActionMessage("doinscricao.ok"));
                //saveErrors(request,messages);
                saveErrors(request,messages);
                return mapping.findForward(SUCCESS); //TODO: colocar uma message aqui e no error tbm
            } else  {
                System.out.println("Erro ao inserir na lista");
                messages.add("log",new ActionMessage("doinscricao.nok"));
                saveErrors(request,messages);
                return mapping.findForward(ERROR);
            }
        } else return null;//TODO: arrumar aqui depois
    }
}
